package com.itlong.cloud.tapd.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <des>
 *
 * <des>
 *
 * @create Date 2021-09-10
 **/
public class Special {

    /**
     * <desc>
     * 【1、统计所有成员近一周完成的工单数】
     * </desc>
     *
     * @param user
     * @param taskList
     * @param storyList
     * @param bugList
     * @return: java.util.HashMap<java.lang.String , java.lang.Integer>
     * @author LiuYu
     * @date: 2021-09-10
     **/
    public static HashMap<String, Integer> getTaskDoneCountInSevenDays(String user, List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        //告警人员列表
        HashMap<String, Integer> map = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Integer taskCount = 0;
        //任务统计
        for (HashMap<String, Object> taskmap : taskList) {
            String completedDate = (String) taskmap.get("completed");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(completedDate)) {
                Date completed = format.parse(completedDate);
                Date SevenDaysAgo = format.parse(GetTime.getNineDaysAgo());
                //completed在最近七天
                if (owner.contains(user) && (SevenDaysAgo).compareTo(completed) <=0) {
                    taskCount++;
                }
            }
        }
        //需求统计
        for (HashMap<String, Object> taskmap : storyList) {
            String completedDate = (String) taskmap.get("completed");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(completedDate)) {
                Date completed = format.parse(completedDate);
                Date SevenDaysAgo = format.parse(GetTime.getNineDaysAgo());
                //completed在最近七天
                if (owner.contains(user) && (SevenDaysAgo).compareTo(completed) <= 0) {
                    taskCount++;
                }
            }
        }
        //缺陷统计
        for (HashMap<String, Object> taskmap : bugList) {
            String completedDate = (String) taskmap.get("resolved");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(completedDate)) {
                Date completed = format.parse(completedDate);
                Date SevenDaysAgo = format.parse(GetTime.getNineDaysAgo());
                //completed在最近七天
                if (owner.contains(user) && (SevenDaysAgo).compareTo(completed) <= 0) {
                    taskCount++;
                }
            }
        }
        map.put("taskCount", taskCount);
        return map;
    }

    /**
     * <desc>
     * 【2、统计所有成员未来一周的open状态的单数】
     * </desc>
     *
     * @param user
     * @param taskList
     * @param storyList
     * @param bugList
     * @return: java.util.HashMap<java.lang.String , java.lang.Integer>
     * @author LiuYu
     * @date: 2021-09-10
     **/
    public static HashMap<String, Integer> getTaskToDoCountInNextSevenDays(String user, List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        HashMap<String, Integer> map = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //告警人员列表
//        任务统计
        Integer taskCount = 0;
        for (HashMap<String, Object> taskmap : taskList) {
            String dueDate = (String) taskmap.get("due");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)) {
                Date due = format.parse(dueDate);
                Date Now = format.parse(GetTime.getNow());
                Date sevenDaysAfter = format.parse(GetTime.getSevenDaysAfter());
                //due在未来七天内
                if (owner.contains(user) && (Now).compareTo(due) <= 0 && (sevenDaysAfter).compareTo(due)>0) {
                    taskCount++;
                }
            }
        }
//        需求统计
        for (HashMap<String, Object> taskmap : storyList) {
            String dueDate = (String) taskmap.get("due");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)) {
                Date due = format.parse(dueDate);
                Date Now = format.parse(GetTime.getNow());
                Date sevenDaysAfter = format.parse(GetTime.getSevenDaysAfter());
                //due在未来七天内
                if (owner.contains(user) && (Now).compareTo(due) <= 0 && (sevenDaysAfter).compareTo(due)>0) {
                    taskCount++;
                }
            }
        }
//        缺陷统计
        for (HashMap<String, Object> taskmap : bugList) {
            String dueDate = (String) taskmap.get("due");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)) {
                Date due = format.parse(dueDate);
                Date Now = format.parse(GetTime.getNow());
                Date sevenDaysAfter = format.parse(GetTime.getSevenDaysAfter());
                //due在未来七天内
                if (owner.contains(user) && (Now).compareTo(due) <= 0 && (sevenDaysAfter).compareTo(due)>0) {
                    taskCount++;
                }
            }
        }
        map.put("taskCount", taskCount);
        return map;
    }

    /**
     * <desc>
     * 【3、统计任务单工作延迟的数据】
     * </desc>
     *
     * @param user
     * @param taskList
     * @param storyList
     * @param bugList
     * @return: java.util.HashMap<java.lang.String , java.lang.Integer>
     * @author LiuYu
     * @date: 2021-09-10
     **/
    public static HashMap<String, Integer> getTaskUnFinishedCount(String user, List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        HashMap<String, Integer> map = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //告警人员列表
//        任务统计
        Integer  taskCount= 0;
        for (HashMap<String, Object> taskmap : taskList) {
            String dueDate = (String) taskmap.get("due");
            String owner = (String) taskmap.get("owner");
            String completedDate = (String) taskmap.get("completed");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)) {
                Date due = format.parse(dueDate);
                Date Now = format.parse(GetTime.getNow());
                //due小于今天，completed没有值
                if (owner.contains(user) && (Now).compareTo(due) > 0 && StringUtils.isBlank(completedDate)) {
                    taskCount++;
                }
            }
        }
//        需求统计
        for (HashMap<String, Object> taskmap : storyList) {
            String dueDate = (String) taskmap.get("due");
            String owner = (String) taskmap.get("owner");
            String completedDate = (String) taskmap.get("completed");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)) {
                Date due = format.parse(dueDate);
                Date Now = format.parse(GetTime.getNow());
                //due小于今天，completed没有值
                if (owner.contains(user) && (Now).compareTo(due) > 0 && StringUtils.isBlank(completedDate)) {
                    taskCount++;
                }
            }
        }
//        缺陷统计
        for (HashMap<String, Object> taskmap : bugList) {
            String dueDate = (String) taskmap.get("due");
            String owner = (String) taskmap.get("owner");
            String completedDate = (String) taskmap.get("resolved");
            if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)) {
                Date due = format.parse(dueDate);
                Date Now = format.parse(GetTime.getNow());
                //due小于今天，completed没有值
                if (owner.contains(user) && (Now).compareTo(due) > 0 && StringUtils.isBlank(completedDate)) {
                    taskCount++;
                }
            }
        }
        map.put("taskCount", taskCount);
        return map;
    }

    /**
     * <desc>
     * 【4、月度单数统计】
     * </desc>
     *
     * @param user
     * @param taskList
     * @param storyList
     * @param bugList
     * @return: java.util.HashMap<java.lang.String , java.lang.Integer>
     * @author LiuYu
     * @date: 2021-09-10
     **/
    public static HashMap<String, Integer> getTaskCountLastMonth(String user, List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        HashMap<String, Integer> map = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //告警人员列表
//        任务统计，正常完成单数和超期完成单数
        Integer taskDoneCount = 0;
        Integer taskDoneInTimeCount = 0;
        Integer taskDelayCount=0;
        for (HashMap<String, Object> taskmap : taskList) {
            String dueDate = (String) taskmap.get("due");
            String completedDate = (String) taskmap.get("completed");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner)) {
                Date firstDayOfLastMonth = format.parse(GetTime.getFirstDayOfLastMonth());
                Date lastDayOfLastMonth = format.parse(GetTime.getLastDayOfLastMonth());
                if(StringUtils.isNotBlank(completedDate)) {
                    Date completed = format.parse(completedDate);
                    //完成单数
                    if (owner.contains(user) && completed.compareTo(firstDayOfLastMonth) >= 0 &&
                            (completed).compareTo(lastDayOfLastMonth) <= 0) {
                        if(owner.contains("张航博")){
                            System.out.println("1completed="+completed);
                            System.out.println("1dueDate"+dueDate);
                        }
                        taskDoneCount++;
                    }
                    //超期完成
                    if(StringUtils.isNotBlank(dueDate)) {
                        Date due = format.parse(dueDate);
                        if (owner.contains(user) && (due).compareTo(firstDayOfLastMonth) >= 0
                                && (due).compareTo(lastDayOfLastMonth) <= 0) {
                            if (completed.compareTo(due) > 0) {
                                taskDoneInTimeCount++;
                            }
                        }
                    }
                }else {
                    //超期未完成
                    if(StringUtils.isNotBlank(dueDate)) {
                        Date due = format.parse(dueDate);
                        if (owner.contains(user) && (due).compareTo(firstDayOfLastMonth) >= 0
                                && (due).compareTo(lastDayOfLastMonth) <= 0) {
                            taskDelayCount++;
                        }
                    }
                }
            }
        }
        //        需求统计，正常完成单数和超期完成单数
        for (HashMap<String, Object> taskmap : storyList) {
            String dueDate = (String) taskmap.get("due");
            String completedDate = (String) taskmap.get("completed");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner)) {
                Date firstDayOfLastMonth = format.parse(GetTime.getFirstDayOfLastMonth());
                Date lastDayOfLastMonth = format.parse(GetTime.getLastDayOfLastMonth());
                if(StringUtils.isNotBlank(completedDate)) {
                    Date completed = format.parse(completedDate);
                    //完成单数
                    if (owner.contains(user) && completed.compareTo(firstDayOfLastMonth) >= 0 &&
                            (completed).compareTo(lastDayOfLastMonth) <= 0) {
                        if(owner.contains("张航博")){
                            System.out.println("2completed="+completed);
                            System.out.println("2dueDate"+dueDate);
                        }
                        taskDoneCount++;
                    }
                    //超期完成
                    if(StringUtils.isNotBlank(dueDate)) {
                        Date due = format.parse(dueDate);
                        if (owner.contains(user) && (due).compareTo(firstDayOfLastMonth) >= 0
                                && (due).compareTo(lastDayOfLastMonth) <= 0) {
                            if (completed.compareTo(due) > 0) {
                                taskDoneInTimeCount++;
                            }
                        }
                    }
                }else {
                    //超期未完成
                    if(StringUtils.isNotBlank(dueDate)) {
                        Date due = format.parse(dueDate);
                        if (owner.contains(user) && (due).compareTo(firstDayOfLastMonth) >= 0
                                && (due).compareTo(lastDayOfLastMonth) <= 0) {
                            taskDelayCount++;
                        }
                    }
                }
            }
        }
        //        缺陷统计，正常完成单数和超期完成单数
        for (HashMap<String, Object> taskmap : bugList) {
            String dueDate = (String) taskmap.get("due");
            String completedDate = (String) taskmap.get("resolved");
            String owner = (String) taskmap.get("owner");
            if (StringUtils.isNotBlank(owner)) {
                Date firstDayOfLastMonth = format.parse(GetTime.getFirstDayOfLastMonth());
                Date lastDayOfLastMonth = format.parse(GetTime.getLastDayOfLastMonth());
                if(StringUtils.isNotBlank(completedDate)) {
                    Date completed = format.parse(completedDate);
                    //完成单数
                    if (owner.contains(user) && completed.compareTo(firstDayOfLastMonth) >= 0 &&
                            (completed).compareTo(lastDayOfLastMonth) <= 0) {
                        if(owner.contains("张航博")){
                            System.out.println("3completed="+completed);
                            System.out.println("3dueDate"+dueDate);
                        }
                        taskDoneCount++;
                    }
                    //超期完成
                    if(StringUtils.isNotBlank(dueDate)) {
                        Date due = format.parse(dueDate);
                        if (owner.contains(user) && (due).compareTo(firstDayOfLastMonth) >= 0
                                && (due).compareTo(lastDayOfLastMonth) <= 0) {
                            if (completed.compareTo(due) > 0) {
                                taskDoneInTimeCount++;
                            }
                        }
                    }
                }else {
                    //超期未完成
                    if(StringUtils.isNotBlank(dueDate)) {
                        Date due = format.parse(dueDate);
                        if (owner.contains(user) && (due).compareTo(firstDayOfLastMonth) >= 0
                                && (due).compareTo(lastDayOfLastMonth) <= 0) {
                            taskDelayCount++;
                        }
                    }
                }
            }
        }
        map.put("taskDoneCount", taskDoneCount);
        map.put("taskDoneInTimeCount", taskDoneInTimeCount);
        map.put("taskDelayCount", taskDelayCount);
        return map;
    }

    /**
     * <desc>
     * 【5、月度缺单统计】
     * </desc>
     *
     * @param user
     * @param taskList
     * @param storyList
     * @param bugList
     * @return: java.util.HashMap<java.lang.String , java.lang.Integer>
     * @author LiuYu
     * @date: 2021-09-10
     **/
    public static HashMap<String, Integer> getMissTaskCountLastMonth(String user, List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        final String[] holiday = {"01-01","01-02","01-03",
                "02-11","02-12","02-13","02-14","02-15","02-16","02-17",
                "04-03","04-04","04-05",
                "05-01","05-02","05-03","05-04","05-05",
                "06-12","06-13","06-14",
                "09-19","09-20","09-21",
                "10-01","10-02","10-03","10-04","10-05","10-06","10-07"};
        HashMap<String, Integer> map = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //告警人员列表
        Integer count = 0,workdayCount=0;
        Calendar calendar =Calendar.getInstance();
        calendar.add(Calendar.MONTH,-1);
        //从上个月一号开始遍历
        for (int i = 1; i <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
            boolean flag = false,flag1=false;
            String date = (new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime())) + "-0" + GetTime.getLastMonth() + "-" + i;
            Date weekDay = format.parse(date);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(weekDay);
            //节假日跳过
            for (int j=0;j<holiday.length;j++){
                if(holiday[j].equals(new SimpleDateFormat("MM-dd").format(calendar1.getTime()))){
                    flag1=true;
                    break;
                }
            }
            if (flag1){
                continue;
            }
            //周六周日跳过
            if ((calendar1.getTime().toString().substring(0,3)).equals("Sat") || (calendar1.getTime().toString().substring(0,3)).equals("Sun")) {
                continue;
            }
            workdayCount++;
//            任务统计
            for (HashMap<String, Object> taskmap : taskList) {
                String dueDate = (String) taskmap.get("due");
                String owner = (String) taskmap.get("owner");
                String createdDate = (String) taskmap.get("begin");
                if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)&&StringUtils.isNotBlank(createdDate)) {
                    Date due = format.parse(dueDate);
                    Date created = format.parse(createdDate);
                    if (owner.contains(user)) {
                        if (weekDay.compareTo(due) <= 0 && weekDay.compareTo(created) >= 0) {
                            flag = true;
                        }
                    }
                }
            }
//            需求统计
            for (HashMap<String, Object> taskmap : storyList) {
                String dueDate = (String) taskmap.get("due");
                String owner = (String) taskmap.get("owner");
                String createdDate = (String) taskmap.get("begin");
                if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)&&StringUtils.isNotBlank(createdDate)) {
                    Date due = format.parse(dueDate);
                    Date created = format.parse(createdDate);
                    if (owner.contains(user)) {
                        if (weekDay.compareTo(due) <= 0 && weekDay.compareTo(created) >= 0) {
                            flag = true;
                        }
                        continue;
                    }
                }
            }
//            缺陷统计
            for (HashMap<String, Object> taskmap : bugList) {
                String dueDate = (String) taskmap.get("due");
                String owner = (String) taskmap.get("owner");
                String createdDate = (String) taskmap.get("begin");
                if (StringUtils.isNotBlank(owner) && StringUtils.isNotBlank(dueDate)&&StringUtils.isNotBlank(createdDate)) {
                    Date due = format.parse(dueDate);
                    Date created = format.parse(createdDate);
                    if (owner.contains(user)) {
                        if (weekDay.compareTo(due) <= 0 && weekDay.compareTo(created) >= 0) {
                            flag = true;
                        }
                        continue;
                    }
                }
            }
            if(!flag){
                count++;
            }
        }
        map.put("count", count);
        map.put("workdayCount",workdayCount);
        return  map;
    }
}
